﻿namespace AgileZen.Models
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    [DataContract(Name = "project")]
    public class Project : AgileZenModel
    {
        private int _id;
        private string _name;
        private string _description;
        private Person _person;
        private string _details;
        private ICollection<Phase> _phases;
        private ICollection<Person> _members;
        private ICollection<Role> _roles;
        private ICollection<Invite> _invites;
        private Metrics _metrics;

        //catch date first in a string, because the JSON serializer expects a Date(*) notation
        [DataMember(Name = "createTime", Order = 0)]
        private string CreatedOnUTCString { get; set; }

        public DateTime? CreatedOn 
        {
            get
            {
                bool parsed = false;
                DateTime datetime = DateTime.MinValue;
                if (!String.IsNullOrEmpty(CreatedOnUTCString))
                {
                    parsed = DateTime.TryParse(CreatedOnUTCString, out datetime);
                }

                return (parsed) ? datetime : new Nullable<DateTime>();
            }
        }

        [DataMember(Name = "id", Order = 2)]
        public int Id 
        {
            get { return _id; }
            set { _id = value; NotifyPropertyChanged("Id"); } 
        }

        public bool ShouldSerializeId()
        {
            return PropertiesChanged.Contains("Id");
        }

        [DataMember(Name = "name", Order = 3, EmitDefaultValue = false)]
        public string Name 
        { 
            get { return _name; } 
            set { _name = value; NotifyPropertyChanged("Name"); } 
        }

        public bool ShouldSerializeName()
        {
            return PropertiesChanged.Contains("Name");
        }

        [DataMember(Name = "description", Order = 1, EmitDefaultValue = false)]
        public string Description 
        {
            get { return _description; }
            set { _description = value; NotifyPropertyChanged("Description"); } 
        }

        public bool ShouldSerializeDescription()
        {
            return PropertiesChanged.Contains("Description");
        }

        [DataMember(Name = "owner", Order = 4, EmitDefaultValue = false)]
        public Person Owner 
        {
            get { return _person; }
            set { _person = value; NotifyPropertyChanged("Owner"); } 
        }

        public bool ShouldSerializeOwner()
        {
            return PropertiesChanged.Contains("Owner");
        }

        [DataMember(Name = "details", Order = 5, EmitDefaultValue = false)]
        public string Details 
        {
            get { return _details; }
            set { _details = value; NotifyPropertyChanged("Details"); } 
        }

        public bool ShouldSerializeDetails()
        {
            return PropertiesChanged.Contains("Details");
        }

        [DataMember(Name = "phases", Order = 6, EmitDefaultValue = false)]
        public ICollection<Phase> Phases 
        {
            get { return _phases; }
            set { _phases = value; NotifyPropertyChanged("Phases"); } 
        }

        public bool ShouldSerializePhases()
        {
            return PropertiesChanged.Contains("Phases");
        }

        [DataMember(Name = "members", Order = 7, EmitDefaultValue = false)]
        public ICollection<Person> Members 
        {
            get { return _members; }
            set { _members = value; NotifyPropertyChanged("Members"); } 
        }

        public bool ShouldSerializeMembers()
        {
            return PropertiesChanged.Contains("Members");
        }

        [DataMember(Name = "roles", Order = 8, EmitDefaultValue = false)]
        public ICollection<Role> Roles 
        {
            get { return _roles; }
            set { _roles = value; NotifyPropertyChanged("Roles"); } 
        }

        public bool ShouldSerializeRoles()
        {
            return PropertiesChanged.Contains("Roles");
        }

        [DataMember(Name = "invites", Order = 9, EmitDefaultValue = false)]
        public ICollection<Invite> Invites 
        {
            get { return _invites; }
            set { _invites = value; NotifyPropertyChanged("Invites"); } 
        }

        public bool ShouldSerializeInvites()
        {
            return PropertiesChanged.Contains("Invites");
        }

        [DataMember(Name = "metrics", Order = 10, EmitDefaultValue = false)]
        public Metrics Metrics 
        {
            get { return _metrics; }
            set { _metrics = value; NotifyPropertyChanged("Metrics"); } 
        }

        public bool ShouldSerializeMetrics()
        {
            return PropertiesChanged.Contains("Metrics");
        }

        public static class Enrichments 
        {
            public const string Details = "details";
            public const string Phases = "phases";
            public const string Members = "members";
            public const string Roles = "roles";
            public const string Invites = "invites";
            public const string Metrics = "metrics";
        }
    }
}
